Socket
Socket
Sign inDemoInstall

web3-eth-abi

Package Overview
Dependencies
2
Maintainers
4
Versions
404
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

web3-eth-abi


Version published
Maintainers
4
Created

Package description

What is web3-eth-abi?

The web3-eth-abi package is a part of the Web3.js library and is used for encoding and decoding Ethereum ABI (Application Binary Interface) data. It allows developers to interact with smart contracts by encoding function calls and decoding the data returned by smart contracts.

What are web3-eth-abi's main functionalities?

Encoding Function Calls

This feature allows you to encode function calls to be sent to the Ethereum blockchain. The code sample demonstrates how to encode a function call with a uint256 and a string parameter.

const Web3 = require('web3');
const web3 = new Web3();
const abi = require('web3-eth-abi');

const functionSignature = 'myMethod(uint256,string)';
const parameters = [123, 'Hello World'];
const encodedData = abi.encodeFunctionCall({
  name: 'myMethod',
  type: 'function',
  inputs: [{ type: 'uint256', name: 'myNumber' }, { type: 'string', name: 'myString' }]
}, parameters);

console.log(encodedData);

Decoding Function Return Values

This feature allows you to decode the return values from a smart contract function call. The code sample demonstrates how to decode a return value that includes a uint256 and a string.

const Web3 = require('web3');
const web3 = new Web3();
const abi = require('web3-eth-abi');

const encodedReturnValue = '0x000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000';
const decodedData = abi.decodeParameters(['uint256', 'string'], encodedReturnValue);

console.log(decodedData);

Encoding Event Logs

This feature allows you to encode event logs for smart contracts. The code sample demonstrates how to encode an event log with a uint256 and a string parameter.

const Web3 = require('web3');
const web3 = new Web3();
const abi = require('web3-eth-abi');

const eventSignature = 'MyEvent(uint256,string)';
const parameters = [123, 'Hello World'];
const encodedEvent = abi.encodeEventSignature({
  name: 'MyEvent',
  type: 'event',
  inputs: [{ type: 'uint256', name: 'myNumber' }, { type: 'string', name: 'myString' }]
});

console.log(encodedEvent);

Decoding Event Logs

This feature allows you to decode event logs from smart contracts. The code sample demonstrates how to decode an event log that includes a uint256 and a string.

const Web3 = require('web3');
const web3 = new Web3();
const abi = require('web3-eth-abi');

const encodedLog = '0x000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000';
const decodedLog = abi.decodeLog([
  { type: 'uint256', name: 'myNumber' },
  { type: 'string', name: 'myString' }
], encodedLog, []);

console.log(decodedLog);

Other packages similar to web3-eth-abi

Readme

Source

web3-eth-abi

NPM Package

This is a sub-package of web3.js.

This is the abi package used in the web3-eth package.

Please read the documentation for more.

Installation

You can install the package either using NPM or using Yarn

Using NPM

npm install web3-eth-abi

Using Yarn

yarn add web3-eth-abi

Usage

const Web3EthAbi = require('web3-eth-abi');

Web3EthAbi.encodeFunctionSignature('myMethod(uint256,string)');
> '0x24ee0097'

Types

All the TypeScript typings are placed in the types folder.

FAQs

Last updated on 05 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc